package cn.yunhe.servlet;

import cn.yunhe.entity.userentity.UserAccount;
import cn.yunhe.entity.userentity.UserAddress;
import cn.yunhe.entity.userentity.UserData;
import cn.yunhe.services.UserAddressServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/DeleteUserAddressServlet")
public class DeleteUserAddressServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        UserAddressServices userAddressServices = new UserAddressServices();

        String adress = request.getParameter("adress"); //1.取数据


        UserAddress userAddress = new UserAddress();
        UserData userData = new UserData();            //2.将数据传递到后台(dao)
        UserAccount userAccount = new UserAccount(1);
        userData.setUserAccount(userAccount);
        userAddress.setUserData(userData);
        userAddress.setAddress(adress);

        int flag = userAddressServices.deleteUserAddress(userAddress);

        PrintWriter out = response.getWriter();
        if (flag>0) {
            response.setContentType("text/html;charset=UTF-8");
            out.print("<script>alert('删除成功');location.href='diZhiZhanShi.jsp';</script>");
//            request.getRequestDispatcher("boardManage.jsp").forward(request, response);
        } else {
//            response.setContentType("text/html;charset=UTF-8");

            out.print("<script>alert('删除地址异常,从新添加');location.href='diZhiZhanShi.jsp';</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
